﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace WellDone
{
    public sealed partial class RefundDialog : Page
    {
        public event EventHandler CloseRequested;
        public bool bCanceled = false;
        public int iCheckNumber = 0, iSelectedIndex = 0;
        public decimal dRefundAmount;
        public string strReason = "";
        public RefundDialog()
        {
            this.InitializeComponent();
        }
        public async Task<Boolean> init()
        {
            await loadChecks();
            return true;
        }
        private async Task<Boolean> loadChecks()
        {
            // Retrieve order summary records for closed checks on selected date
            string strFilter = "Status=" + Globals.k_int_status_closed.ToString() +
                " and TimeOpened between '" + dpSelectedDate.Date.Date.ToString() + "' and '" + dpSelectedDate.Date.Date.AddDays(1).ToString() + "'";
            Globals.OrderSummaryRecords = await Globals.client.GetOrderSummaryObjectsAsync(Globals.token, strFilter);
            cmbCheckNumber.Items.Clear();
            if (Globals.OrderSummaryRecords.Count > 0)
            {
                // Retrieve list of order summary IDs
                string strOrderSummaryIDs = "";
                foreach (ServiceReferenceOpenRPOS.OrderSummary os in Globals.OrderSummaryRecords)
                    strOrderSummaryIDs += os.col_ID.ToString() + ",";
                strOrderSummaryIDs = strOrderSummaryIDs.Substring(0, strOrderSummaryIDs.Length - 1);
                // Get checks associated with selected summary records
                Globals.OrderCheckRecords = await Globals.client.GetOrderCheckObjectsAsync(Globals.token, "OrderSummaryID in (" + strOrderSummaryIDs + ")");
                // Add these checks to the combo box
                foreach (ServiceReferenceOpenRPOS.OrderCheck oc in Globals.OrderCheckRecords)
                    cmbCheckNumber.Items.Add(oc.col_ID.ToString());
                if (Globals.OrderCheckRecords.Count > 0)
                {
                    cmbCheckNumber.SelectedIndex = 0;
                    decimal dTotal = Globals.OrderCheckRecords[0].col_Subtotal + Globals.OrderCheckRecords[0].col_Tax + Globals.OrderCheckRecords[0].col_Gratuity;
                    tblkTotal.Text = dTotal.ToString("N");
                }
            }
            return true;
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            // Set the check number
            if (cmbCheckNumber.SelectedItem != null)
            {
                iSelectedIndex = cmbCheckNumber.SelectedIndex;
                iCheckNumber = Globals.OrderCheckRecords[iSelectedIndex].col_ID;
            }
            // Set the refund amount and reason
            try
            {
                dRefundAmount = Convert.ToDecimal(tbRefundAmount.Text);
            }
            catch (FormatException)
            {
                dRefundAmount = 0;
            }
            strReason = tbReason.Text;
            // Close the dialog
            if (this.CloseRequested != null)
            {
                this.CloseRequested(this, EventArgs.Empty);
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            // Set "Canceled" 
            bCanceled = true;
            if (this.CloseRequested != null)
            {
                this.CloseRequested(this, EventArgs.Empty);
            }
        }

        private async void DatePicker_DateChanged(object sender, DatePickerValueChangedEventArgs e)
        {
            // Re-load the checks for selected date
            await loadChecks();
        }

        private void cmbCheckNumber_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbCheckNumber.SelectedIndex!=-1)
            {
                // Update the total for the current selection...
                decimal dTotal = Globals.OrderCheckRecords[cmbCheckNumber.SelectedIndex].col_Subtotal +
                    Globals.OrderCheckRecords[cmbCheckNumber.SelectedIndex].col_Tax +
                    Globals.OrderCheckRecords[cmbCheckNumber.SelectedIndex].col_Gratuity;
                tblkTotal.Text = dTotal.ToString("N");
            } 
        }
    }
}
